import axios, {
  type AxiosInstance,
  type AxiosRequestConfig,
  type AxiosResponse,
} from "axios";
import { showNotify } from "vant";
import { ElMessage } from "element-plus";
let instance: AxiosInstance = axios.create({
  baseURL: "/api",
  timeout: 5000,
});
instance.interceptors.request.use((config: AxiosRequestConfig) => {
  let token = localStorage.getItem("token");
  if (token) {
    (config.headers as any).token = token;
  }
  return config;
});
instance.interceptors.response.use((res: AxiosResponse) => {
  if (res.data?.data?.token) {
    localStorage.setItem("token", res.data.data.token);
  }
  showNotify({
    type: "success",
    message: `${res.data.message}`,
    duration: 1000,
  });
  return res.data;
});
export default instance;
